// pages/collect/collect.js
var app = getApp();

Page({
  data: {
    chose: true,
    pad: "0px",
    bianji: '编辑',
    isAllSelect: false,
    haschose: false,
    arr: [],
    sellers: []
  },
  // 点击进店
  goshop: function () {
    wx.navigateTo({
      url: '../store/store',
    })
  },
  start_chose: function () {
    var that = this;
    var bianji = this.data.bianji;
    var sellers = that.data.sellers;
    if (bianji == '编辑') {
      that.setData({
        pad: '60rpx',
        bianji: '完成',
        chose: false
      })
    } else {
      that.setData({
        pad: '0rpx',
        bianji: '编辑',
        chose: true
      })
    }
  },
  //选择
  switchSelect: function (e) {
    let that     = this;
    var cid      = e.currentTarget.id;
    var haschose = that.data.haschose;
    var index    = e.currentTarget.dataset.index;
    var sellers  = that.data.sellers;
    var arr      = that.data.arr;
    sellers[index].status = !sellers[index].status
    arr          = arr.concat(sellers[index])
    that.setData({
      sellers: sellers,
      arr: arr
    })
    console.log(that.data.sellers)
  },
  //全选
  allSelect: function (e) {
    let that = this;
    var haschose = that.data.haschose;
    var sellers = that.data.sellers;
    var isAllSelect = !that.data.isAllSelect;
    if (isAllSelect) {
      for (var i = 0; i < sellers.length; i++) {
        console.log(i)
        sellers[i].status = true
      }
      that.setData({
        sellers: sellers,
      })
    } else {
      for (var i = 0; i < sellers.length; i++) {
        sellers[i].status = false
      }
      that.setData({
        sellers: sellers,
      })
    }
    that.setData({
      isAllSelect: isAllSelect
    })
  },
  //删除
  delete: function (e) {
    let that = this
    var arr = that.data.arr
    var sellers = that.data.sellers
    var cid     = '';
    for (var i = 0; i < sellers.length; i++){
      if(that.data.isAllSelect) {
        cid += sellers[i]['cid'] + ','
        // sellers.splice(0, sellers.length)
      }else if(sellers[i].status == true){
        cid += sellers[i]['cid'] + ','
        // for(var n = 0; n < arr.length; n++){
        //   sellers.splice(i, 1)
        // }
        // that.setData({
        //   arr: []
        // })
      // }else{
      //   let that = this;
      //   var sellers = that.data.sellers;
      //   var arr = that.data.arr
      }
    }
    wx.request({
      url: app.url + '/User/collect_del',
      method: 'post',
      data: {
        cid :cid,
        uid:wx.getStorageSync('uid')
      },
      success: function (res) {

      }
    })
    // that.setData({
    //   sellers: sellers,
    // });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      url: app.url + '/User/collect_shop',
      method :'post',
      data: { uid: wx.getStorageSync('uid')},
      success: function(res){
        if(res.data.code){ that.setData({ sellers: res.data.data }) } 
      },
      error  : function(){}
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})